package com.common.help;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Test12 {
	
	public static void main(String[] args) {
        ListMap map=new ListMap();
        map.put("a","111");
        map.put("v","190");
        map.put("d","132");
        //利用自己建立的方法getValue（）保证输出有序
        for(int i=0;i<map.size();i++){
            System.out.println(map.getValue(i));
        }
        
        System.out.println();
        
        //利用从HashMap中继承的方法values（）输出不保证有序
        String[] list=(String[])map.values().toArray(new String[0]);
        for (int i = 0; i < list.length; i++) {
            System.out.println(list[i]);
        }
    }
}

class ListMap extends HashMap  {

    private static final long serialVersionUID = -6040757858724463221L;
    private List list;
    public ListMap(){
        super();
        this.list=new ArrayList();
    }
    
    //重写父类中的方法put在此映射中关联指定值与指定键
    public Object put(Object key,Object value)
    {
        if(list.contains(key))
        {
            list.remove(key);
        }
        this.list.add(key);
        return super.put(key,value);
    }
    
    public Object getKey(int i){
        return this.list.get(i);
    }
    
    //获得对应键的值
    public Object getValue(int i){
        return this.get(getKey(i));
    }
    
    public int size()
    {
        return this.list.size();
    }
}